﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AmaroK86.MassEffect3.PCC;
using AmaroK86.MassEffect3.UnrealObjects;
using AmaroK86.MassEffect3;

namespace provaobj
{
    class Program
    {
        static void Main(string[] args)
        {
            PCCObject pccFile;

            pccFile = new PCCObject(ME3Paths.cookedPath + @"AreaMap_CitHub_Camps.pcc");

            // searches throught all export data in pcc
            foreach (PCCObject.ExportEntry export in pccFile.Exports)
            {
                UnrealObject uObj = new UnrealObject(export);

                foreach (UnrealType property in uObj.properties)
                {
                    switch (property.TypeName)
                    {
                        case "IntProperty": Console.WriteLine(property.Name + " it's an Int, has value \"" + property.Value + "\"\n"); break;
                        case "FloatProperty": Console.WriteLine(property.Name + " it's a Float, has value \"" + property.Value + "\"\n"); break;
                        case "BoolProperty": Console.WriteLine(property.Name + " it's a Bool, has value \"" + property.Value + "\"\n"); break;
                        case "NameProperty": Console.WriteLine(property.Name + " it's a Name, has value \"" + property.Value + "\"\n"); break;
                        case "ByteProperty": Console.WriteLine(property.Name + " it's a Byte, his main value is \"" + property.Value + "\" and his secondary value is \"" + (property as ByteType).val2 + "\"\n"); break;
                    }
                }
            }
            Console.WriteLine("now part 2, editing properties...");
            Console.ReadLine();

            // test 2:
            pccFile = new PCCObject(ME3Paths.cookedPath + @"BioA_CitHub_Persidium.pcc");

            //searching for any texture
            int texIdx = pccFile.Exports.FindIndex(export => export.ClassName == "Texture2D");
            UnrealObject tex2D = new Texture2D(pccFile.Exports[texIdx]);

            //reading and editing values!
            Console.WriteLine("Editing property values!");

            string propertyName = "Format";
            Console.WriteLine("  editing " + propertyName + " property");
            Console.WriteLine("    Original string property: " + tex2D.properties[propertyName].Value);
            tex2D.properties[propertyName].Value = "PF_DXT1"; // editing the value
            Console.WriteLine("    Modified string property: " + tex2D.properties[propertyName].Value);
            Console.WriteLine();

            propertyName = "SizeX";
            Console.WriteLine("  editing " + propertyName + " property");
            Console.WriteLine("    Original string property: " + tex2D.properties[propertyName].Value);
            tex2D.properties[propertyName].Value = 666; // editing the value
            int somevalue = (int)tex2D.properties[propertyName].Value; // getting the value from properties (needs a cast)
            Console.WriteLine("    Modified string property: " + somevalue + "\n");

            Console.WriteLine("now part 3, editing a derived unrealObject...");
            Console.ReadLine();

            // test 1: open all files called BioA_CitHub_Persidium*.pcc and read all textures
            List<string> fileList = Directory.GetFiles(ME3Paths.cookedPath, "BioA_CitHub_Persidium*.pcc", SearchOption.AllDirectories).ToList();

            foreach (string inputPath in fileList)
            {
                pccFile = new PCCObject(inputPath);

                for (int i = 0; i < pccFile.Exports.Count; i++)
                {
                    switch (pccFile.Exports[i].ClassName)
                    {
                        case "Texture2D":
                            {
                                UnrealObject UObject = new Texture2D(pccFile.Exports[i]);
								
								// do editing here...

                                Console.WriteLine("saving " + UObject.exportRef.ObjectName + " object");
                                UObject.commitChanges(); // save changes
                                break;
                            }
                        default:
                            break;
                    }
                }
                if (pccFile.isModified)
                {
                    Console.WriteLine("  now finally saving " + Path.GetFileName(inputPath));
                    //pccFile.saveToFile(true); // save pcc file
                    Console.WriteLine("    " + Path.GetFileName(inputPath) + " saved!\n\nDone. press any key to exit.");
                }
            }

            Console.ReadLine();
			
        }
    }
}
